package com.sxg.controller;

import com.sxg.convert.SxgOrderItemConvert;
import com.sxg.entity.SxgOrderItemEntity;
import com.sxg.query.SxgOrderItemQuery;
import com.sxg.vo.SxgOrderItemVO;
import lombok.AllArgsConstructor;
import com.sxg.page.PageResult;
import com.sxg.utils.code.Result;
import com.sxg.service.SxgOrderItemService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import jakarta.validation.Valid;
import java.util.List;

/**
* 订单商品表
*
* @author 浪漫写尽歌谱 3298751420@qq.com
* @since 1.0.0 2024-03-07
*/
@RestController
@RequestMapping("sxg/sxg_order_item")
@AllArgsConstructor
public class SxgOrderItemController {
    private final SxgOrderItemService sxgOrderItemService;

    @GetMapping("page")
    @PreAuthorize("hasAuthority('sxg:sxg_order_item:page')")
    public Result<PageResult<SxgOrderItemVO>> page(@Valid SxgOrderItemQuery query){
        PageResult<SxgOrderItemVO> page = sxgOrderItemService.page(query);

        return Result.ok(page);
    }

    @GetMapping("{id}")
    @PreAuthorize("hasAuthority('sxg:sxg_order_item:info')")
    public Result<SxgOrderItemVO> get(@PathVariable("id") Long id){
        SxgOrderItemEntity entity = sxgOrderItemService.getById(id);

        return Result.ok(SxgOrderItemConvert.INSTANCE.convert(entity));
    }

    @PostMapping
    @PreAuthorize("hasAuthority('sxg:sxg_order_item:save')")
    public Result<String> save(@RequestBody SxgOrderItemVO vo){
        sxgOrderItemService.save(vo);

        return Result.ok();
    }

    @PutMapping
    @PreAuthorize("hasAuthority('sxg:sxg_order_item:update')")
    public Result<String> update(@RequestBody @Valid SxgOrderItemVO vo){
        sxgOrderItemService.update(vo);

        return Result.ok();
    }

    @DeleteMapping
    @PreAuthorize("hasAuthority('sxg:sxg_order_item:delete')")
    public Result<String> delete(@RequestBody List<Long> idList){
        sxgOrderItemService.delete(idList);

        return Result.ok();
    }
}